package com.itheima.test;

import com.itheima.pojo.User;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.xml.crypto.Data;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Chenke Zhao
 * @date 2025/5/23 11:24
 * @description POIDemo3    读取一个excel文件
 */
public class POIDemo3 {

    public static void main(String[] args) throws IOException, ParseException {
        //  读取一个excel文件
       //  用户名	手机号	省份	城市	工资	入职日期	出生日期	现住地址
        Workbook workbook = new XSSFWorkbook(new FileInputStream("D:\\用户导入测试数据.xlsx"));
        // 读取第一个sheet
        Sheet sheet = workbook.getSheetAt(0);
        // 获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        Row row = null;
        //格式化日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        User user = null;
        for (int i = 1; i <= lastRowNum; i++) {
           row = sheet.getRow(i);
            if (row != null) {
                String username = row.getCell(0).getStringCellValue();
                String phone = row.getCell(1).getStringCellValue();
                String province = row.getCell(2).getStringCellValue();
                String city = row.getCell(3).getStringCellValue();
                double salary = row.getCell(4).getNumericCellValue();
                Date hireDate =  simpleDateFormat.parse(row.getCell(5).getStringCellValue());
                Date birthday = simpleDateFormat.parse(row.getCell(6).getStringCellValue());
                String address = row.getCell(7).getStringCellValue();
                user = new User(username,phone,province,city,salary,hireDate,birthday,address);
                System.out.println("user = " + user);
                System.out.println("=======================");
            }
        }


    }
}
